/*  
	Author      : Sidek Sirun
	Course      : UCI Java Programming I (X460.10)
	Assignment 4: Extra Credit 

	Update getOldestPerson() method to use a loop to step through an array of collection to find oldest person.

*/

import java.util.ArrayList;

class MyCollectionTest {
	/**
	* getOldestPerson() method
	*/
	public static void getOldestPerson(ArrayList<Person> personArray) {		
		ArrayList<Person> pa = new ArrayList<Person>(personArray);
		int oldestAge        = 0;
		int count            = 0;
		String oldestPerson  = "";
		
		// find oldest age
		for (int i = 0; i < pa.size(); i++) {
			if (oldestAge < pa.get(i).getAge()) {
				oldestAge    = pa.get(i).getAge();
				//oldestPerson = pa[i].getFullName();
			}
		}

		// find oldest person (if there are more than one) once the oldest age is known
		for (int j = 0; j < pa.size(); j++) {
			if (oldestAge == pa.get(j).getAge()) {
				count++;
				if (count == 1) {
					oldestPerson = pa.get(j).getFullName();
				} else {
					oldestPerson = oldestPerson + ", " + pa.get(j).getFullName();
				}
			}
		}

		System.out.println("Oldest age: " + oldestAge);
		System.out.println("No. of oldest person: " + count);
		if (count == 1) {
			System.out.println("Oldest person: " + oldestPerson);
		} else {
			System.out.println("Oldest persons: " + oldestPerson);
		}
	}
	/**
	* @main() method
	*/
	public static void main(String[] args) {
		// New collection for Person class that utilize Generics
		ArrayList<Person> personArray = new ArrayList<Person>();

		System.out.println("");
		System.out.println("===== MyCollectionTest =====");
		System.out.println("Size of PersonArray (before adding element): " + personArray.size());

		// Add new elements
		Person p0 = new Person("James",  "Bond",  49, "Male",   "011-70-7777");
		Person p1 = new Person("Lara",   "Croft", 25, "Female", "033-55-8888");
		Person p2 = new Person("Austin", "Power", 51, "Male",   "001-03-9999");
		Person p3 = new Person("Mary",   "Jane",  51, "Female", "010-05-9000");
		Person p4 = new Person("Clark",  "Kent",  39, "Male",   "100-50-1000");

		personArray.add(p0);
		personArray.add(p1);
		personArray.add(p2);
		personArray.add(p3);
		personArray.add(p4);

		// Try adding non-Person type
		//String t0 = "testABC";
		//personArray.add(t0);

		System.out.println("");
		System.out.println("Size of PersonArray (after adding element): " + personArray.size());

		// Print to STDOUT
		System.out.println("Collection contents:");
		for (int i = 0; i < personArray.size(); i++) {
			System.out.println("Person " + i + " info => " + personArray.get(i));
		}

		// Remove element
		//System.out.println("");
		//System.out.println("Removing element #3 => " + p2.toString());
		//personArray.remove(p2);
		//System.out.println("Size of PersonArray (after removing element): " + personArray.size());
		//System.out.println("Collection contents:");
		//for (int i = 0; i < personArray.size(); i++) {
		//	System.out.println("Person " + i + " info => " + personArray.get(i));
		//}

		// Update element
		//System.out.println("");
		//Person p5 = new Person("Jen", "Law", 23, "Female", "202-22-2552");
		//System.out.println("Updating element #4 with => " + p5.toString());
		//personArray.set(3, p5);
		//System.out.println("Size of PersonArray (after updating element): " + personArray.size());
		//System.out.println("Collection contents:");
		//for (int i = 0; i < personArray.size(); i++) {
		//	System.out.println("Person " + i + " info => " + personArray.get(i));
		//}

		System.out.println("");
		System.out.println("PersonTest getOldestPerson() method:");
		getOldestPerson(personArray);

		System.out.println("");
	}

}